﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.Windows.Data;
using System.ComponentModel;

namespace EIA.SLClient.ProjectManage.Contract
{
    public class ProjectContractViewModel : SimpleMvvmToolkit.ViewModelDetailBase<ProjectContractViewModel, EIA.PM.Model.TProjectContract>
	{
		public ProjectContractViewModel()
		{

        }

        private void Busy(string msg = "等待...")
        {
            IsBusy = true;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>(msg, true));
        }

        private void Free()
        {
            IsBusy = false;
            SendMessage<bool>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.BUSY_STATE, new SimpleMvvmToolkit.NotificationEventArgs<bool>("", false));
        }

        #region notifyprop

        protected bool _IsBusy;
        /// <summary>
        /// 
        /// </summary>
        public bool IsBusy
        {
            get { return _IsBusy; }
            set
            {
                _IsBusy = value;
                NotifyPropertyChanged(m => m.IsBusy);
            }
        }

        protected bool _IsNew;
        /// <summary>
        /// 是否为添加操作
        /// </summary>
        public bool IsNew
        {
            get { return _IsNew; }
            set
            {
                _IsNew = value;
                NotifyPropertyChanged(m => m.IsNew);
            }
        }

        protected EIA.PM.Model.TProjectBasic _ProjectBasic;
        /// <summary>
        /// 注释
        /// </summary>
        public EIA.PM.Model.TProjectBasic ProjectBasic
        {
            get { return _ProjectBasic; }
            set
            {
                _ProjectBasic = value;
                NotifyPropertyChanged(m => m.ProjectBasic);
            }
        }

        protected EIA.Basic.Model.DicItem _HTQDZT;
        /// <summary>
        /// 合同主体
        /// </summary>
        public EIA.Basic.Model.DicItem HTQDZT
        {
            get { return _HTQDZT; }
            set
            {
                _HTQDZT = value;
                NotifyPropertyChanged(m => m.HTQDZT);
                if(_HTQDZT != null)Model.PrjContractSignedAs = _HTQDZT.DicIid;
            }
        }
        
        #endregion

        #region command

        #region EditCommand

        private SimpleMvvmToolkit.DelegateCommand _EditCommand = null;
        public SimpleMvvmToolkit.DelegateCommand EditCommand
        {
            get
            {
                if (_EditCommand == null)
                {
                    _EditCommand = new SimpleMvvmToolkit.DelegateCommand(Edit, CanExcuteEdit);
                } return _EditCommand;
            }
            set { _EditCommand = value; NotifyPropertyChanged(m => m.EditCommand); }
        }

        public void Edit()
        {
            BeginEdit();
        }

        public bool CanExcuteEdit()
        {
            return true;
        }

        #endregion

        #region CancelCommand

        private SimpleMvvmToolkit.DelegateCommand _CancelCommand = null;
        public SimpleMvvmToolkit.DelegateCommand CancelCommand
        {
            get
            {
                if (_CancelCommand == null)
                {
                    _CancelCommand = new SimpleMvvmToolkit.DelegateCommand(Cancel, CanExcuteCancel);
                } return _CancelCommand;
            }
            set { _CancelCommand = value; NotifyPropertyChanged(m => m.CancelCommand); }
        }

        public void Cancel()
        {
            CancelEdit();
        }

        public bool CanExcuteCancel()
        {
            return true;
        }

        #endregion

        #region SaveCommand

        private SimpleMvvmToolkit.DelegateCommand _SaveCommand = null;
        public SimpleMvvmToolkit.DelegateCommand SaveCommand
        {
            get
            {
                if (_SaveCommand == null)
                {
                    _SaveCommand = new SimpleMvvmToolkit.DelegateCommand(Save, CanExcuteSave);
                } return _SaveCommand;
            }
            set { _SaveCommand = value; NotifyPropertyChanged(m => m.SaveCommand); }
        }

        public void Save()
        {
            EIA.PM.BLL.ClientProxy.TProjectContractProxy proxy = new EIA.PM.BLL.ClientProxy.TProjectContractProxy();
            Busy();
            if (IsNew)
            {
                proxy.Insert(Model, (k) => {
                    Free();
                    IsNew = false;
                    Model.ContractId = k;
                    EndEdit();
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("创建合同信息发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
            else
            {
                proxy.Update(Model, (r) =>
                {
                    Free();
                    EndEdit();
                }, (ex) =>
                {
                    Free();
                    System.Windows.MessageBox.Show("更新合同信息发生错误！", "提示", System.Windows.MessageBoxButton.OK);
                });
            }
        }

        public bool CanExcuteSave()
        {
            return true;
        }

        #endregion

        #endregion

        public void OnLoad()
        {
            this.SendMessage<object, EIA.PM.Model.TProjectBasic>(EIA.SLClient.ProjectManage.Infrastructure.MessageTokens.PROJECT_CURRENT, new SimpleMvvmToolkit.NotificationEventArgs<object, PM.Model.TProjectBasic>("", null, (p) =>
            {
                ProjectBasic = p;
            }));
            if (IsEditing) CancelEdit();
            Model = null;
            Busy();
            EIA.PM.BLL.ClientProxy.TProjectContractProxy proxy = new EIA.PM.BLL.ClientProxy.TProjectContractProxy();
            Dictionary<string,object> con = new Dictionary<string,object>();
            con[EIA.PM.Model.TProjectContract.PRJ_ID] = ProjectBasic.PrjId;
            proxy.List(con, 1, 1, (lst) => {
                Free();
                if (lst == null || lst.Count == 0)
                {
                    this.Model = new PM.Model.TProjectContract() { PrjId = ProjectBasic.PrjId,  PrjContractAmountRate = 1, PrjWorkAmountRate = 1};
                    IsNew = true;
                }
                else
                {
                    this.Model = lst[0];
                    IsNew = false;
                }
                HTQDZT = ListsResource.Instance.HTQDZTCollection.FirstOrDefault((o) => { return o.DicIid == this.Model.PrjContractSignedAs; });
            }, (ex) => {
                Free();
            });
        }
	}
}